package com.zxc.gulimall.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.zxc.common.exception.BizCodeEnum;
import com.zxc.gulimall.member.entity.MemberReceiveAddressEntity;
import com.zxc.gulimall.member.exception.PhoneExistsException;
import com.zxc.gulimall.member.exception.UsernameExistException;
import com.zxc.gulimall.member.feign.CouponFeignService;
import com.zxc.gulimall.member.service.MemberReceiveAddressService;
import com.zxc.gulimall.member.vo.MemberLoginVo;
import com.zxc.gulimall.member.vo.MemberRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zxc.gulimall.member.entity.MemberEntity;
import com.zxc.gulimall.member.service.MemberService;
import com.zxc.common.utils.PageUtils;
import com.zxc.common.utils.R;



/**
 * 会员
 *
 * @author zxc
 * @email 2898743173@qq.com
 * @date 2021-10-27 11:26:51
 */
@RestController
@RequestMapping("member/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;

    @GetMapping(value = "/{memberId}/address")
    public List<MemberReceiveAddressEntity> getAddress(@PathVariable(value = "memberId")Long memberId){
        List<MemberReceiveAddressEntity> memberReceiveAddressEntities = memberReceiveAddressService.getAddress(memberId);
        return memberReceiveAddressEntities;
    }

    @PostMapping(value = "/login")
    public R login(@RequestBody MemberLoginVo vo){
        MemberEntity memberEntity = memberService.login(vo);
        if (memberEntity != null){
            return R.ok().put("data",memberEntity);
        }else {
            return R.error(BizCodeEnum.LOGINACCOUNT_PASSWORD_INVAILD_EXCEPTION.getCode(),BizCodeEnum.LOGINACCOUNT_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }
    }


    @PostMapping(value = "/register")
    public R register(@RequestBody MemberRegisterVo vo){
        try {
            memberService.register(vo);
        }catch (PhoneExistsException e){
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
        }catch (UsernameExistException e){
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(),BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }


    @RequestMapping(value = "/coupons")
    public R test(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("周绪驰");
        R memberCoupons = couponFeignService.memberCoupons();
        return R.ok().put("member",memberEntity).put("coupons",memberCoupons);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
